package net.pimpas.web.jsf.renderers.util.html;

import java.beans.PropertyDescriptor;
import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.ResponseWriter;

import net.pimpas.logging.serviceprovider.LoggingServiceProvider;
import net.pimpas.logging.serviceprovider.LoggingServiceProviderFactoryImpl;
import net.pimpas.util.reflection.ReflectionUtil;

public abstract class HtmlRendererUtils {
	
	private static final LoggingServiceProvider logger = 
		LoggingServiceProviderFactoryImpl.getLoggingServiceProvider(HtmlRendererUtils.class);

	public static void writeHtmlAttribute(UIComponent cp, ResponseWriter writer, String propertyName,
			String attributeName) {
		
		PropertyDescriptor property = ReflectionUtil.getPropertyDescriptor(cp.getClass(), propertyName);
		Object value = null;
		if(property != null)
			try {
				value = property.getReadMethod().invoke(cp);
			} catch (Throwable e) {
				logger.error("Cannot read property ["+propertyName+"] for component "+cp, e);
			}
		if(value != null)
			try {
				writer.writeAttribute(attributeName, value, null);
			} catch (IOException e) {
				logger.error("Cannot write attribute ["+attributeName+"] for component "+cp, e);
			}
	}
}